package rin.android.gamble;



import rin.android.database.ClassPlayer;
import rin.android.database.Database;
import rin.android.dialog.MessageDialog;
import rin.android.sound.MusicBackground;


import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class TaiXiu extends MyApp {
	ImageView img1,img2,img3,img_tai,img_xiu;
	TextView tv,tv_taixiu, tv_player, tv_dollar,tv_ketqua;
	Button btn;
	int random;
	boolean flag = false;
	boolean taixiu;	
	boolean stop = false;
	int mang_hinh_xx[]={
			R.drawable.xx1,R.drawable.xx2,R.drawable.xx3,R.drawable.xx4,R.drawable.xx5,R.drawable.xx6
	};
	AnimationDrawable animation1;
	AnimationDrawable animation2;
	AnimationDrawable animation3;
	String userName;
	String playerDollar;
	int diem;
	Database mData;
	ClassPlayer mPlayer;
	MusicBackground mMusic;
	MessageDialog dialog;
	Context context;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.taixiu);
		mMusic = new MusicBackground();
		mMusic.loadMusic(this);
	    if (Setting.isMusic)
	        mMusic.play();
	    else{
	        mMusic.play();
	        mMusic.pause();
	    }
		dialog = new MessageDialog();
		context = this;
		img1 	=  (ImageView) findViewById(R.id.xingau1);
		img2 	=  (ImageView) findViewById(R.id.xingau2);
		img3 	=  (ImageView) findViewById(R.id.xingau3);
		img_tai =  (ImageView) findViewById(R.id.tai);
		img_xiu =  (ImageView) findViewById(R.id.xiu);
		
		tv 		=  (TextView) findViewById(R.id.quangcao);
		tv_taixiu	=  (TextView) findViewById(R.id.dattaixiu);
		tv_player = (TextView) findViewById(R.id.player_taixiu);
		tv_dollar = (TextView) findViewById(R.id.dollar_taixiu);
		tv_ketqua = (TextView) findViewById(R.id.ketqua_taixiu);
		btn		=  (Button)	  findViewById(R.id.bt_do_xi_ngau);
		Bundle extras = getIntent().getExtras();
        //Nếu có dữ liệu thì lấy đưa vào chuỗi với key tương ứng
        userName = extras.getString("userName");
        playerDollar = extras.getString("dollar");
        diem = Integer.parseInt(playerDollar);
        tv_player.setText("Player: "+userName);
		tv_dollar.setText("$$$ "+playerDollar);
		showAnimation();
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
					if(flag){
						if(diem <100){
							dialog.showKhongDuTien(context);
							return;
						}
						doXiNgau();
					}
					else {
						dialog.showChonTaiXiu(context);
						MainActivity.mSound.playRandom();
					}
			}
		});		
		
		img_tai.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MainActivity.mSound.playClick();
				// TODO Auto-generated method stub
				tv_taixiu.setText("Bạn đã đặt TÀI");
				flag = true;
				taixiu = true;
				
			}
		});
		
		img_xiu.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MainActivity.mSound.playClick();
				tv_taixiu.setText("Bạn đã đặt XỈU");
				flag = true;
				taixiu = false;
			}
		});
	}
	public int soNgauNhien()
	{
		return random = (int) (Math.random()*6);
	}
	public void showAnimation(){
		if (img1 != null) {
			img1.setVisibility(View.VISIBLE);			
			animation1 = (AnimationDrawable) img1.getDrawable();
			animation1.setCallback(img1);
			animation1.setVisible(true, true);
		}
		if (img2 != null) {
			img2.setVisibility(View.VISIBLE);			
			animation2 = (AnimationDrawable) img2.getDrawable();
			animation2.setCallback(img2);
			animation2.setVisible(true, true);
		}
		if (img3 != null) {
			img3.setVisibility(View.VISIBLE);			
			animation3 = (AnimationDrawable) img3.getDrawable();
			animation3.setCallback(img3);
			animation3.setVisible(true, true);
		}
	}
	public void doXiNgau()
	{
		// Khai báo các biến chứa giá trị xx1,xx2,xx3 và tổng nút
		int value1,value2,value3,sonut;		
		//Gán số nút cho xí ngầu
		value1 = soNgauNhien();
		value2 = soNgauNhien();
		value3 = soNgauNhien();
		img1.setImageResource(mang_hinh_xx[value1]);
		img2.setImageResource(mang_hinh_xx[value2]);
		img3.setImageResource(mang_hinh_xx[value3]);
		
		//Tổng nút
		sonut = value1+value2+value3+3;
		
		//Xuất kết quả tài xỉu
		if((taixiu & sonut>9) || (!taixiu & sonut<=9))
		{
			MainActivity.mSound.playGood();
			dialog.showWinDialog(this);	
			diem += 50;
			tv_dollar.setText("$$$ " + diem + "");
		}
		else
		{	
			MainActivity.mSound.playBad();	
			dialog.showLoseDialog(this);
			diem -= 100;
			tv_dollar.setText("$$$ " + diem + "");
		}
			
		//Xuất kết quả ra textView
		tv_ketqua.setText("Số nút là :"+sonut);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public void onBackPressed(){
		super.onBackPressed();
		MainActivity.mSound.playClick();
		mData = new Database(this);
        mData.openDatabase();
        mPlayer = mData.getPlayerByName(userName);
		mPlayer.setDollar(diem);
		mData.updatePlayer(mPlayer);
		mData.closeDatabase();
		Intent i = new Intent(this, MainActivity.class);
		i.putExtra("userName", mPlayer.getName());
		startActivity(i);
		finish();
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		mMusic.release();
	}
	public void onPause(){
		super.onPause();
		mMusic.pause();
	}
	public void onResume(){
		super.onResume();
		if(Setting.isMusic) mMusic.resume();
	}
}
